﻿using System.Collections.Generic;
using System.Linq;
using LBoL.Base;
using LBoL.Base.Extensions;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.Core.Cards;
using LBoL.EntityLib.StatusEffects.Others;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;

namespace ProbeMod.Cards
{
    /// <summary>
    /// 护盾电池
    /// </summary>
    public sealed class ProbeShieldBatteryDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Common;
            config.Type = CardType.Friend;
            config.TargetType = TargetType.Nobody;
            config.Colors = new List<ManaColor>() { ManaColor.Blue };

            config.Cost = new ManaGroup() { Any = 1, Blue = 1 };
            config.UpgradedCost = new ManaGroup() { Any = 1, Blue = 1 };

            config.Loyalty = 4;
            config.UpgradedLoyalty = 4;

            //  被动耐久变化
            config.PassiveCost = -1;
            config.UpgradedPassiveCost = -1;

            config.ActiveCost = -2;
            config.UpgradedActiveCost = -2;

            //  大招消耗耐久.
            config.UltimateCost = -6;
            config.UpgradedUltimateCost = -6;

            config.Block = 8;
            config.UpgradedBlock = 10;

            config.RelativeEffects = new List<string>() { "ProbeBuildingKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeBuildingKeywordSe" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    [EntityLogic(typeof(ProbeShieldBatteryDef))]
    public sealed class ProbeShieldBattery : ProbeFriendCard
    {
        public int GainUnity => -ActiveCost;
        public int ActiveBlock
        {
            get => IsUpgraded ? Config.UpgradedBlock.Value * 2 : Config.Block.Value * 2;
        }

        /// <summary>
        /// 被动：-1并且获得8/10格挡
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<BattleAction> GetPassiveActions()
        {
            //Triigger the effect only if the card has been summoned. 
            if (!base.Summoned || base.Battle.BattleShouldEnd)
            {
                yield break;
            }
            base.NotifyActivating();
            //Increase base loyalty.
            if(base.Loyalty >0)
            {
                base.Loyalty += base.PassiveCost;
                for (int i = 0; i < base.Battle.FriendPassiveTimes; i++)
                {
                    if (base.Battle.BattleShouldEnd)
                    {
                        break;
                    }
                    yield return DefenseAction();
                }
            }
            yield break;
        }

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            if (precondition == null || ((MiniSelectCardInteraction)precondition).SelectedCard.FriendToken == FriendToken.Active)
            {
                base.Loyalty += base.ActiveCost;
                yield return new CastBlockShieldAction(Battle.Player, Battle.Player, ActiveBlock, 0, BlockShieldType.Normal, true);

                if(Battle.HandZone.Any((Card card) => card.CardType == CardType.Friend && card != this))
                {
                    var handFriend = Battle.HandZone.Where((Card card) => card.CardType == CardType.Friend && card != this).Sample(BattleRng);
                    if (handFriend != null)
                    {
                        handFriend.Loyalty -= ActiveCost;
                    }
                }
                yield break;
            }

            base.Loyalty += base.UltimateCost;
            base.UltimateUsed = true;
            var block = IsUpgraded ? Config.Block * 3 : Config.UpgradedBlock * 3;
            yield return new CastBlockShieldAction(Battle.Player, Battle.Player, GameRun.Player.MaxHp, 0 , BlockShieldType.Normal, true);
        }
    }
}
